<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        #id{
            width: 100%;
        }
        #left{
            width:40%;
            height:400px;
            /*float:left;*/
            margin:0 auto;
             /*background-color: red;*/
            /*position: fixed;*/
0
        }
        #left #login{
            margin-left:25%;
            margin-top: 180px;
        }
        #left input{
            padding-left: 5px;
            width:250px;
            height:35px;
            border-radius: 10px;
            outline:none;
        }
        #left p{
            font-size: 20px;
        }
        #left  #submit{
            margin-top: 20px;
            margin-left:170px;
            width:260px;
            height: 40px;
            background-color: rgb(226, 148, 52);
            color: white;
            font-size: 20px;
            border:3px solid rgb(226, 148, 52);
            border-radius: 10px;
        }

    </style>
</head>
<body>
<div id="main">
    <div id="left">
        <!-- <p style="margin:0 auto;">登录</p> -->
        <form action="" id="login">
            <h1 style="margin-left:100px;">登录</h1>
            <p>手机号码：</p><input type="num" maxlength="11" oninput="value=value.replace(/[^\d]/g,'')" id="phone">
            <p>密码：</p><input type="password"  id="password"><br>
            <div id="add"></div>
            <b><a href="register.html">点击注册</a></b>
        </form>
        <button id="submit" onclick="bbb();aaa()">登 录</button>

    </div>
</div>
</body>
<script>
    // var path = "http://192.168.0.27:8080";
    // var path = "http://192.168.0.14:8080";
    var path = "http://192.168.1.116:8080";


    //var submit = document.getElementById('submit');
    var phone = document.getElementById('phone');
    var password = document.getElementById('password');
    function isPhone(pone) {
        var myreg = /^1[0-9]{10}$/;
        if(!myreg.test(pone)) {
            return false;
        }else{
            return true;
        }
    }
    function aaa(){
        if(phone.value ==''||password.value == ''){
            alert('输入框不能为空！！');
        }else{
            let flag = isPhone(phone.value);
            if(!flag){
                alert("请填写11位手机号码！！");
            }else{
                alert('登录成功！！')
            }
        }
    }
    var phone = document.getElementById('phone');
    var password = document.getElementById('password');
    function bbb() {
        var xhr = new XMLHttpRequest();
        var data = "phone=" + phone.value + "&password=" + password.value;
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4) {
                if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
                    var res = xhr.responseText;
                    judge(res);
                    console.log(res)
                } else {
                    alert("request is error!!" + xhr.status);
                }
            }
        }
        xhr.open('post',path+'/user/login',true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send(data);
    }
    function judge(a) {
        if (a == "true") {
            alert("登陆成功！正在跳转。。。");
            // window.location.href = "main.html";
            window.location.href = path+"/user/main.html";

        } else {
            alert("登陆失败！！！您可能还未注册！");
        }
    }

</script>
</html>